<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="queryPageList(), searchPage()">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">批次编号</div>
            <el-input v-model="mnecode" clearable />
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">养殖鱼塘</div>
            <el-select filterable clearable v-model="fishPondId" filterable clearable>
              <el-option v-for="(item, index) in fishPondList" :key="index" :label="item.pondName"
                :value="item.fishPondId" />
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">养殖品种</div>
            <el-select filterable clearable v-model="fishVarietyId" filterable clearable>
              <el-option v-for="(item, index) in FishVarietyData" :key="index" :label="item.varietyName"
                :value="item.fishVarietyId" />
            </el-select>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">封塘日期</div>
            <!-- <el-date-picker
              v-model="buildDateTime"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              clearable
            />-->
            <el-date-picker v-model="closePondDateTimeList" type="daterange" placeholder="选择日期" format="yyyy-MM-dd"
              value-format="yyyy-MM-dd" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" clearable />
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar></template>
    <template v-slot:content>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border
        height="calc(100% - 50px)" :row-key="row => {
          return row.productionBatchId;
        }
          " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <!-- <el-table-column type="selection" width="55" :reserve-selection="true" :selectable="(row) => { return !row.selectable }" /> -->
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.productionBatchId ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column prop="mnecode" label="批次编号" />
        <el-table-column prop="productionBatchCode" label="码单号" />
        <el-table-column prop="breedTypeName" label="养殖类型" />
        <el-table-column prop="varietyName" label="养殖鱼种" />
        <el-table-column prop="pondName" label="养殖鱼塘" />
        <el-table-column prop="pondRatio" label="鱼塘占比(%)" />
        <el-table-column prop="num" label="批次剩余数量（尾）" />
        <el-table-column prop="weight" label="均重（斤）" />
        <el-table-column prop="supplier" label="供应商">
          <template v-if="scoped.row.pondRatio" slot-scope="scoped">
            <span>{{ scoped.row.supplier ? processName(scoped.row.supplier) : '--' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="buildDateTime" label="创建时间" />
        <el-table-column prop="closePondDateTime" label="封塘日期">
          <template v-if="scoped.row.productionBatchId" slot-scope="scoped">
            <div class="table-btns-box">
              {{ scoped.row.closePondDateTime ? scoped.row.closePondDateTime : '--' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template v-if="scoped.row.productionBatchId" slot-scope="scoped">
            <div class="table-btns-box">
              <div class="table-btns edit-btns" @click="complete(scoped.row)">
                <p>完成</p>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
        @changeCurrentPage="changeCurrentPage" />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new2.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import BatchMangeApi from "@/api/batch-mange";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety";
import FishPondApi from "@/api/basic-info-manage/fishPond";
export default {
  name: "BatchComplete",
  components: { ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data () {
    return {
      closePondDateTimeList: [],
      mnecode: "", //批次编号
      productionBatchCode: "", //码单号
      fishPondId: "", //养殖鱼塘ID
      fishVarietyId: "", //养殖品种ID
      buildDateTime: "", //批次创建时间
      checkedIdList: [],
      fishPondList: [], //全部鱼塘
      FishVarietyData: [], //全部品种
      showAddEditDialog: false,
      // activeName: "1"
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created () {
    this.queryPageList();
    this.queryFishVariety();
    this.queryFishPond();
  },
  mounted () {
    // console.log(this);
  },
  methods: {
    searchPage () {
      this.currentPage = 1
      this.queryPageList();
    },
    queryPageList () {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          status: "UN_COMPLETE"
        }
      };
      if (this.mnecode) params.entity.mnecode = this.mnecode;
      if (this.productionBatchCode)
        params.entity.productionBatchCode = this.productionBatchCode;
      if (this.fishPondId) params.entity.fishPondId = this.fishPondId;
      if (this.fishVarietyId) params.entity.fishVarietyId = this.fishVarietyId;
      if (this.closePondDateTimeList && this.closePondDateTimeList.length > 0) {
        console.log(this.closePondDateTimeList)
        params.entity.closePondDateTimeStart = this.closePondDateTimeList[0] + " 00:00:00"
        params.entity.closePondDateTimeEnd = this.closePondDateTimeList[1] + " 23:59:59"
      }
      BatchMangeApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // 完成操作
    complete (row) {
      this.$confirm("您确定完成批次吗?", "完成批次操作", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      })
        .then(() => {
          BatchMangeApi.complete({
            productionBatchId: row.productionBatchId
          }).then(res => {
            if (res.success === true) {
              this.$message.success("完成批次!");
              this.queryPageList();
              this.resetActionBar();
            } else if (res.errCode === 200) {
              this.$message.success(res.message);
            } else {
              this.$message.success("系统异常，请稍后再试");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //查询可选鱼类品种
    queryFishVariety () {
      FishVarietyApi.queryByCondition().then(res => {
        this.FishVarietyData = res.data
      });
    },
    //查询可选鱼塘
    queryFishPond () {
      FishPondApi.queryByCondition().then(res => {
        this.fishPondList = res.data
      });
    },
    changeCurrentPage (currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.productionBatchId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.productionBatchId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange (list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.productionBatchId) {
          return item.productionBatchId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 添加
    add () {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit (row) {
      if (row.productionBatchId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-range-separator {
  color: #333333;
}

.select-box-content-name {
  min-width: 80px;
}

.select-box-content {
  width: none !important;
}
</style>
